package com.yearno.baseframe.base

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.viewbinding.ViewBinding
import com.yearno.baseframe.databinding.BaseActivityBinding
import com.yearno.baseframe.dialog.LoadingDialogFragment


abstract class BaseActivity<VB : ViewBinding> : AppCompatActivity() {

    var baseBinding: BaseActivityBinding? = null
    var viewBinding: VB? = null
    protected abstract fun getVB(): VB

    private val mLoadingDialog: LoadingDialogFragment by lazy {
        LoadingDialogFragment()
    }

    protected fun showLoading() {
        if (!mLoadingDialog.isAdded) {
            mLoadingDialog.show(supportFragmentManager, "loading")
        }
    }

    protected fun dismissLoading() {
        if (mLoadingDialog.isVisible) {
            mLoadingDialog.dismiss()
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        baseBinding = BaseActivityBinding.inflate(layoutInflater)
        setContentView(baseBinding?.root)
        viewBinding = getVB()
    }

    override fun onDestroy() {
        super.onDestroy()
        baseBinding = null
        viewBinding = null
    }
}